﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web.UI;

namespace CustomControlPractice.Controls.DropDownList
{
    public partial class StylishDropDownList : UserControl, IPostBackEventHandler
    {
        private const string SelectedIndexChangedEventArgument = "SelectedIndexChanged";
        private const int DefaultSelectedIndex = 0;
        protected string SelectedIndexChangedEventReference = string.Empty;
        private static readonly object EventSelectedIndexChanged = new object();
        
        protected override void  OnInit(EventArgs e)
        {
 	        base.OnInit(e);
            SelectedIndexChangedEventReference = Page.ClientScript.GetPostBackEventReference(this, SelectedIndexChangedEventArgument);
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            if (eventArgument.Equals(SelectedIndexChangedEventArgument))
            {
                RaiseEvent();
            }
        }

        private void RaiseEvent()
        {
            var eventHandler = (EventHandler<EventArgs>)Events[EventSelectedIndexChanged];
            if (eventHandler != null) eventHandler(this, EventArgs.Empty);
        }

        #region public properties/methods
        /// <summary>
        /// Selected index changed event
        /// </summary>
        [Category("Action")]
        public event EventHandler<EventArgs> SelectedIndexChanged
        {
            add { Events.AddHandler(EventSelectedIndexChanged, value); }
            remove { Events.RemoveHandler(EventSelectedIndexChanged, value); }
        }

        /// <summary>
        /// Whether enable post back when selected index changed
        /// </summary>
        [Category("Behavior")]
        public bool EnablePostBack
        {
            get
            {
                return string.IsNullOrEmpty(enablePostBack.Value) ? false : bool.Parse(enablePostBack.Value);
            }
            set
            {
                enablePostBack.Value = value.ToString();
            }
        }

        /// <summary>
        /// Data source
        /// </summary>
        public IList<KeyValuePair<string, string>> DataSource { get; set; }

        /// <summary>
        /// Bind data
        /// </summary>
        public override void DataBind()
        {
            base.DataBind();
            var serializer = new DataContractJsonSerializer(typeof(IList<KeyValuePair<string, string>>));
            var memoryStream = new MemoryStream();
            serializer.WriteObject(memoryStream, DataSource);
            dataSource.Value = Encoding.UTF8.GetString(memoryStream.ToArray());
        }

        /// <summary>
        /// Selected value
        /// </summary>
        public string SelectedValue
        {
            get { return selectedValue.Value; }
            set { selectedValue.Value = value; }
        }

        /// <summary>
        /// Selected text
        /// </summary>
        public string SelectedText
        {
            get { return selectedText.Value; }
        }

        /// <summary>
        /// Selected index
        /// </summary>
        public int SelectedIndex
        {
            get
            {
                int index; 
                if(int.TryParse(selectedIndex.Value,out index))
                {
                    return index;
                }
                return DefaultSelectedIndex;
            }
        }
        #endregion


    }
}